package com.djmw.mouse.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import com.djmw.mouse.entity.CvLang;
import com.djmw.mouse.repository.CvLangDao;

//Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class CvLangService {

	@Autowired
	private CvLangDao cvLangDao;

	public CvLang getCvLang(Long id) {
		return cvLangDao.findOne(id);
	}

	@Transactional(readOnly = false)
	public void saveCvLang(CvLang entity) {
		cvLangDao.save(entity);
	}

	@Transactional(readOnly = false)
	public void deleteCvLang(Long id) {
		cvLangDao.delete(id);
	}

	public Page<CvLang> getCvLangList(Long cvId) {
		PageRequest pageRequest = new PageRequest(0, 100, new Sort(Direction.ASC, "lang"));
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		filters.put("cv.id", new SearchFilter("cv.id", Operator.EQ, cvId));
		Specification<CvLang> spec = DynamicSpecifications.bySearchFilter(filters.values(), CvLang.class);
		return cvLangDao.findAll(spec, pageRequest);
	}

}
